/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package system;

import sql.SQLHandler;
import java.util.ArrayList;

/**
 *
 * @author Micke
 */
public class GradeHandler {

    SQLHandler sql;
    ArrayList<Grade> grades = new ArrayList<Grade>();
    MovieHandler movies;
    Movie selectedMovie;
    private boolean addedFav;
    private boolean removedFav;
    String lastFavouriteChanged;

    public GradeHandler(SQLHandler sql, MovieHandler movies) {
        this.sql = sql;
        this.movies = movies;
        if (!sql.isConnected()) {
            sql.Connect();
        }
        getGrades();
        addedFav = false;

    }

    public boolean getGrades() {
        try {
            grades = sql.getGrades();
            return true;
        } catch (NullPointerException e) {
            System.out.println("Nullpointers i getGrade!!!");
        }
        return false;
    }

    public boolean isFavourited() {
        return addedFav;
    }

    public void toggleAddFavourite(boolean favourited) {
        this.addedFav = favourited;
    }

    public String getLastFavourited() {
        return lastFavouriteChanged;
    }

    public void setLastFavourited(String lastFavourited) {
        this.lastFavouriteChanged = lastFavourited;
    }

    public Grade getAGrade(int movieID, int userID) {
        return sql.getAUserGrade(movieID, userID);
    }

    public String[] getTitleListFavourites(ArrayList<Movie> movies, int userID) {
        String[] titles = new String[sql.getLengthOfFavourites(userID)];
        int k = 0;
        for (int i = 0; i < movies.size(); i++) {
            for (int j = 0; j < grades.size(); j++) {
                if (movies.get(i).getMovieID() == grades.get(j).getMovieID() && grades.get(j).isFavourite) {
                    titles[k] = movies.get(i).getTitle();
                    k++;
                }
            }
        }
        return titles;
    }

    public boolean isMovieFavourite(int movieID, int userID) {



        return sql.getFavourite(movieID, userID);
    }

    public String[] getTopTitles() {
        ArrayList<String> titles = new ArrayList<String>();
        ArrayList<Movie> movieList = movies.getMoviesArray();
        float topGrade = 5;


        while (titles.size() <= 10) {
            for (int i = 0; i < movieList.size(); i++) {
                if (sql.getAverageGrade(movieList.get(i).getMovieID()) == topGrade) {
                    titles.add(movieList.get(i).getTitle());
                }

            }
            topGrade-=0.1;
        }
        String[] temp = new String[10];
        for (int i = 0; i < temp.length; i++) {
            temp[i]=titles.get(i);
        }
        return temp;
    }

    public void setFavourite(int movieID, int userID, boolean state) {
        sql.setFavourite(movieID, userID, state);
        System.out.println("Favourite=" + state + ", movieID=" + movieID + ", userID=" + userID);

        lastFavouriteChanged = movies.getMovieTitleByMovieID(movieID);
    }

    public float getAverageGrade(int movieID) {
        return sql.getAverageGrade(movieID);
    }

    public void gradeAMovie(int movieID, int userID, float grade) {
        for (int i = 0; i < grades.size(); i++) {
            if (grades.get(i).getMovieID() == movieID && grades.get(i).getUserID() == userID) {
                grades.get(i).setGrade(grade);
                sql.updateUserGrade(movieID, userID, grade);
                updateAverageGrade(movieID);
                System.out.println("Grade a movie, movieID=" + movieID + ", userID=" + userID + " grade=" + grade);
            }
        }
    }

    public double getUserMovieGrade(int movieID, int userID) {
        for (int i = 0; i < grades.size(); i++) {
            if (grades.get(i).getMovieID() == movieID && grades.get(i).getUserID() == userID) {
                return grades.get(i).getGrade();
            }
        }
        return -1;
    }

    public void updateAverageGrade(int movieID) {
        int k = 0;
        boolean send = false;
        float grade = 0;
        for (int i = 0; i < grades.size(); i++) {
            if (grades.get(i).getMovieID() == movieID && grades.get(i).getGrade() != 0) {
                k++;
                grade += grades.get(i).getGrade();
                send = true;
            }
        }
        if (send) {
            grade = grade / k;
            sql.updateAvgGrade(movieID, grade);
        }
    }
}
